package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.TUserWalletVo;
import org.dromara.system.domain.bo.TUserWalletBo;
import org.dromara.system.service.ITUserWalletService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户钱包
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userWallet")
public class TUserWalletController extends BaseController {

    private final ITUserWalletService tUserWalletService;

    /**
     * 查询用户钱包列表
     */
    @SaCheckPermission("system:userWallet:list")
    @GetMapping("/list")
    public TableDataInfo<TUserWalletVo> list(TUserWalletBo bo, PageQuery pageQuery) {
        return tUserWalletService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户钱包列表
     */
    @SaCheckPermission("system:userWallet:export")
    @Log(title = "用户钱包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TUserWalletBo bo, HttpServletResponse response) {
        List<TUserWalletVo> list = tUserWalletService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户钱包", TUserWalletVo.class, response);
    }

    /**
     * 获取用户钱包详细信息
     *
     * @param walletId 主键
     */
    @SaCheckPermission("system:userWallet:query")
    @GetMapping("/{walletId}")
    public R<TUserWalletVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long walletId) {
        return R.ok(tUserWalletService.queryById(walletId));
    }

    /**
     * 新增用户钱包
     */
    @SaCheckPermission("system:userWallet:add")
    @Log(title = "用户钱包", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TUserWalletBo bo) {
        return toAjax(tUserWalletService.insertByBo(bo));
    }

    /**
     * 修改用户钱包
     */
    @SaCheckPermission("system:userWallet:edit")
    @Log(title = "用户钱包", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TUserWalletBo bo) {
        return toAjax(tUserWalletService.updateByBo(bo));
    }

    /**
     * 删除用户钱包
     *
     * @param walletIds 主键串
     */
    @SaCheckPermission("system:userWallet:remove")
    @Log(title = "用户钱包", businessType = BusinessType.DELETE)
    @DeleteMapping("/{walletIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] walletIds) {
        return toAjax(tUserWalletService.deleteWithValidByIds(List.of(walletIds), true));
    }
}
